package com.bignerdranch.android.expresshelper.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.bignerdranch.android.expresshelper.Fragment.First;
import com.bignerdranch.android.expresshelper.Fragment.Fourth;
import com.bignerdranch.android.expresshelper.Fragment.Second;
import com.bignerdranch.android.expresshelper.Fragment.Third;
import com.bignerdranch.android.expresshelper.R;


import java.util.ArrayList;
import java.util.List;

public class GuiderActivity extends AppCompatActivity {
    private ViewPager mPager;
    private List<Fragment> mList;

    private boolean isFirst1;
    private SharedPreferences sharedPreferences;
    private final static String DEFAULT_FILE_NAME = "GuiderActivity";


    @Override
    protected void onStop() {
        super.onStop();
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * 判断是否第一次登录
         */
        sharedPreferences = getSharedPreferences(DEFAULT_FILE_NAME, MODE_PRIVATE);
        isFirst1 = sharedPreferences.getBoolean("isFirst",true);
        if(isFirst1){
            SharedPreferences.Editor eidt=  sharedPreferences.edit();
            eidt.putBoolean("isFirst",false);
            eidt.commit();

            setContentView(R.layout.activity_guider);
            initPager();

        }else {
            startActivity(new Intent(GuiderActivity.this,MainActivity.class));
            this.finish();
        }

    }

  /*  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guider);
        initPager();

    }*/

    private void initPager() {
        mList = getFragment();
        mPager = (ViewPager)this.findViewById(R.id.pager);
        FragmentManager fm = getSupportFragmentManager();
        mPager.setAdapter(new FragmentStatePagerAdapter(fm) {
            @Override
            public Fragment getItem(int position) {
                return mList.get(position);
            }

            @Override
            public int getCount() {
                return mList.size();
            }
        });
    }

    private List<Fragment> getFragment(){
        List<Fragment> list = new ArrayList<>();
        list.add(new First());
        list.add(new Second());
        list.add(new Third());
        list.add(new Fourth());


        return list;
    }
}
